package service

import (
	"fmt"
	"github.com/vueadmin/app/admin/internal/dto"
	"github.com/vueadmin/app/admin/internal/model"
	"github.com/vueadmin/app/admin/internal/svc"
	"github.com/vueadmin/app/admin/internal/vo"
	"github.com/vueadmin/utils/conv"
)

type supplierService struct{
	svcCtx *svc.ServiceContext
}

func NewSupplierService(svcCtx *svc.ServiceContext) *supplierService {
	return &supplierService{
		svcCtx: svcCtx,
	}
}

/**
 * @description(数据列表)
 * @buildcode(true)
*/
func (p supplierService) GetPageList(req *dto.SupplierPageReq) ([]*vo.SupplierList, int64, error) {
	var (
		entity model.Supplier
		list  []*vo.SupplierList
		count int64
	)
	query := p.svcCtx.Db.Model(&entity)
	if !conv.IsEmpty(req.SupplierId) {
		query = query.Where("supplier_id = ?", req.SupplierId)
	}
	if !conv.IsEmpty(req.SupplierName) {
		query = query.Where("supplier_name = ?", req.SupplierName)
	}
	if !conv.IsEmpty(req.Status) {
		query = query.Where("status = ?", req.Status)
	}
	if !conv.IsEmpty(req.Username) {
		query = query.Where("username = ?", req.Username)
	}
	if !conv.IsEmpty(req.CreateTime){
		query = query.Where("create_time BETWEEN ? AND ?", conv.UnixTime(req.CreateTime[0]), conv.UnixTime(req.CreateTime[1]))
	}
	orderBy := "supplier_id desc"
	if !conv.IsEmpty(req.Sort) && !conv.IsEmpty(req.Order) {
		orderBy = fmt.Sprintf("%s %s", req.Sort, req.Order)
	}
	query.Count(&count)
	if err := query.Limit(req.Limit).Offset((req.Page - 1) * req.Limit).Order(orderBy).Find(&list).Error; err != nil {
		return nil, 0, err
	}
	return list, count, nil
}

/**
 * @description(修改排序开关)
 * @buildcode(true)
*/
func (p supplierService) UpdateExt(req map[string]interface{}) error {
	entity := model.Supplier{}
	if err := p.svcCtx.Db.Model(&entity).Where("supplier_id",conv.Int(req["supplier_id"])).Updates(req).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(添加)
 * @buildcode(true)
*/
func (p supplierService) Add(req *dto.SupplierAddReq) (int, error) {
	entity := model.Supplier{}
	entity.SupplierName = req.SupplierName
	entity.Status = req.Status
	entity.Username = req.Username
	entity.Password = conv.EncryptPassword(req.Password)
	entity.CreateTime = conv.NowUnix()

	if err := p.svcCtx.Db.Create(&entity).Error; err != nil {
		return 0, err
	}
	return entity.SupplierId, nil
}

/**
 * @description(修改)
 * @buildcode(true)
*/
func (p supplierService) Update(req *dto.SupplierUpdateReq) error {
	entity := model.Supplier{
		SupplierId: req.SupplierId,
	}
	query := p.svcCtx.Db
	if err := query.Limit(1).First(&entity).Error; err != nil {
		return err
	}
	entity.SupplierName = req.SupplierName
	entity.Status = req.Status
	entity.Username = req.Username
	entity.CreateTime = conv.UnixTime(req.CreateTime)

	field := conv.StringToSlice("supplier_name,status,username,create_time", ",")

	if err := query.Select(field).Updates(&entity).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(获取修改详情)
 * @buildcode(true)
*/
func (p supplierService) GetUpdateInfo(req *dto.SupplierGetUpdateInfoReq) (*model.Supplier, error) {
	entity := model.Supplier{}
	if err := p.svcCtx.Db.Where("supplier_id = ?",req.SupplierId).Limit(1).First(&entity).Error; err != nil {
		return nil, err
	}
	return &entity, nil
}

/**
 * @description(删除)
 * @buildcode(true)
*/
func (p supplierService) Delete(req *dto.SupplierDeleteReq) error {
	query := p.svcCtx.Db
	query = query.Where("supplier_id IN ?", req.SupplierId)
	if err := query.Delete(&model.Supplier{}).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(查看详情)
 * @buildcode(true)
*/
func (p supplierService) Detail(req *dto.SupplierDetailReq) (*vo.SupplierDetail, error) {
	var info vo.SupplierDetail
	query := p.svcCtx.Db.Model(&model.Supplier{})
	if err := query.Where("supplier_id = ?",req.SupplierId).Limit(1).First(&info).Error; err != nil {
		return nil, err
	}
	return &info, nil
}

/**
 * @description(重置密码)
 * @buildcode(true)
*/
func (p supplierService) ResetPwd(req *dto.SupplierResetPwdReq) error {
	entity := model.Supplier{
		SupplierId: conv.Int(req.SupplierId),
	}
	query := p.svcCtx.Db
	if err := query.Limit(1).First(&entity).Error; err != nil {
		return err
	}
	if err := query.Model(&entity).Update("password",conv.EncryptPassword(req.Password)).Error; err != nil {
		return err
	}
	return nil
}

